import httpx
from bs4 import BeautifulSoup

def mtproxyz(response): # Parser for mtpro.xyz
    proxies = []
    soup = BeautifulSoup(response, 'html.parser')
    for i in soup.find_all('a'):
        try:
            for j in i.get("href").split():
                if "tg://proxy?" in j:
                    url = j.replace("amp;", "").replace("tg://proxy","https://t.me/proxy")
                    proxies.append({"url": url, "ping": None, "up": False})
        except:
            pass

    return proxies

def tme(response): # Parser for telegram`s channels
    proxies = []
    soup = BeautifulSoup(response, 'html.parser')
    for i in soup.find_all('a'):
        try:
            for j in i.get("href").split():
                if "https://t.me/proxy?" in j:
                    url = j.replace("amp;", "")
                    proxies.append({"url": url, "ping": None, "up": False})
        except:
            pass

    return proxies

def fetch(url):
    return httpx.get(url).text # Get website to parse proxy

def get_proxies():

    urls = ["https://mtpro.xyz/mtproto","https://t.me/s/mtprotolists", "https://t.me/s/proxymtproto", "https://t.me/s/mtproxystar", "https://t.me/s/CatalogTelegram"]
    proxies = []

    for url in urls:

        response = fetch(url)

        if url == "https://mtpro.xyz/mtproto":
            proxies += mtproxyz(response)

        if "t.me" in url:
            proxies += tme(response)


    return proxies
